package com.ptszyxx.popose.module.main.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.FragmentMineWorldBinding;
import com.ptszyxx.popose.module.main.mine.vm.MineWorldVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.mine.VipUserEntity;
import com.ysg.http.data.entity.user.UserResult;
import com.ysg.utils.YStringUtil;
import com.ysg.widget.pic.YPicUtils;

import java.util.ArrayList;
import java.util.List;

public class MineWorldFragment extends BaseFragment<FragmentMineWorldBinding, MineWorldVM> {
    public static MineWorldFragment getInstance() {
        return new MineWorldFragment();
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mine_world;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MineWorldVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MineWorldVM.class);
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.requestWallet();
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestUser();
        viewModel.requestVip();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onVipEvent.observe(this, o -> setVipUI());
        viewModel.uc.onAvatarZoomEvent.observe(this, o -> onAvatarZoom());
//        viewModel.uc.onAvatarEditEvent.observe(this, o -> showDialogAvatarEdit());
    }

    /**
     * vip
     */
    private void setVipUI() {
        VipUserEntity user = viewModel.vipResult.getUser();
        if (user == null){
            return;
        }
        String isVip = user.getIsVip();
        if (YStringUtil.eq("1", isVip)) {
            binding.tvVipTip.setText(String.format(getResources().getString(R.string.mine_vip_status_open), user.getVipEndDate()));
            binding.tvVipOpen.setText(getResources().getString(R.string.renew));
        } else {
            binding.tvVipTip.setText(getResources().getString(R.string.mine_vip_tip));
            binding.tvVipOpen.setText(getResources().getString(R.string.open));
        }
    }

    private void onAvatarZoom() {
        UserResult userResult = viewModel.entity.get();
        if (userResult != null && YStringUtil.isNotEmpty(userResult.getPic())){
            List<String> list = new ArrayList<>();
            list.add(userResult.getPic());
            YPicUtils.getInstance().zoomPic(getContext(), list, 0);
        }
    }


}
